#include "LkkRccInit.h"

void Lkk_Clock_Config(void){
	Lkk_Rcc *lkk_Rcc = (Lkk_Rcc *) 0x40021000UL; 
	Lkk_FLASH *lkk_FlASH = (Lkk_FLASH *) 0x40022000UL;
	Lkk_Pwr *lkk_PWR = (Lkk_Pwr*)0x40007000UL;
	/**
		Rcc_CR init
	**/
    lkk_Rcc->rcc_CR.HSION = 0x1; //open HSI
	lkk_Rcc->rcc_CR.HSIRDY = 0x1;//ready HSI
	
	
	
	lkk_Rcc->rcc_CR.HSITRIM = 0x10;//HSI adjustment
	lkk_Rcc->rcc_CR.HSICAL = 0x0;//HSI correcting
	
	//close HSE
	lkk_Rcc->rcc_CR.HSEON = 0x0;
	lkk_Rcc->rcc_CR.HSERDY = 0x0;
	lkk_Rcc->rcc_CR.HSEBYP = 0x0;
	lkk_Rcc->rcc_CR.CSSON = 0x0;
	
	
	/**
		PLL frequency doubling coefficient
		The PLL frequency doubling coefficient is determined by software settings. 
		It can only be written when PLL is off
	**/
	lkk_Rcc->rcc_CR.PLLON = 0x0; 
	lkk_Rcc->rcc_CFGR.PLLMUL = 0x0E;

	
	/**
		Rcc_CFGR init
	**/
	
	lkk_Rcc->rcc_CFGR.SW = 0x02; //System clock switching
	lkk_Rcc->rcc_CFGR.SWS = 0x02;
	
	lkk_Rcc->rcc_CFGR.HPRE = 0x0;	//AHB Prescaler
	lkk_Rcc->rcc_CFGR.PPRE1 = 0x04;	//APB1 Prescaler
	lkk_Rcc->rcc_CFGR.PPRE2 = 0x0;	//APB2 Prescaler
	lkk_Rcc->rcc_CFGR.ADCPRE = 0x0;	//ADC Prescaler
	lkk_Rcc->rcc_CFGR.PLLSRC = 0x0;	//PLL input clock source
	
	lkk_Rcc->rcc_CFGR.MCO = 0x0;	//MCO microcontroller clock output : None
	
	//Rcc_AHBENR init
	lkk_Rcc->rcc_AHBENR.SRAMEN = 0x1;	//SRAM clock enable
	
	
	
	/**
		FLASH
	**/

	lkk_FlASH->lkk_flash_ACR.LATENCY = 0x02;
	lkk_FlASH->lkk_flash_ACR.PRFTBE = 0x0;
	
	lkk_Rcc->rcc_AHBENR.FLITFEN = 0x1;	//Flash memory interface circuit clock enable
	
	//Rcc_APB2ENR init
	lkk_Rcc->rcc_APB2ENR.AFIOEN = 0x1; //Auxiliary function IO clock enable
	lkk_Rcc->rcc_APB2ENR.IOPAEN = 0x1;	//IO port a clock enable
	lkk_Rcc->rcc_APB2ENR.IOPCEN = 0x1;	//IO port c clock enable
	lkk_Rcc->rcc_APB2ENR.IOPDEN = 0x1;	//IO port d clock enable
	
	//Rcc_APB1ENR init
	lkk_Rcc->rcc_APB1ENR.PWREN = 0x1;
	
	
	//Rcc_BDCR init
	lkk_Rcc->rcc_BDCR.RTCSEL = 0x0;
	
	//Rcc_CSR init
	lkk_Rcc->rcc_CSR.PINRSTF = 0x1;
	lkk_Rcc->rcc_CSR.PORRSTF = 0x1;
	
	
	lkk_Rcc->rcc_CR.PLLON = 0x1;
	

	
	//PWR
	
	lkk_PWR->pwr_CR.PLS = 0x00;
	
}


